package com.tys.algorithm.bak.code1;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.TreeMap;

public class Code19_HashMapAndSortedMap {
    public static class Node {
        public int value;

        public Node(int v) {
            value = v;
        }
    }

    public static class Zuo {
        public int value;

        public Zuo(int v) {
            value = v;
        }
    }

    public static void printMap(Map<Integer, String> map) {
        System.out.println("===========开始打印map===========");
        for (Map.Entry entry : map.entrySet()) {
            System.out.print(entry.getKey() + " ");
//            System.out.print(entry.getKey() + ":" + entry.getValue() + "  ");
        }
        System.out.println();
        System.out.println("===========结束打印map===========");
    }

    public static void main(String[] args) {

        //Integer 类型不能使用 == 比较
        Integer a = 1900000;
        Integer b = 1900000;
        Integer e = 127;
        Integer f = 127;
        System.out.println("注意Integer ==: " + (e == f)); //true -128 到 127
        System.out.println("注意Integer ==: " + (a == b)); //false
        System.out.println("注意Integer ==: " + (a == 1900000)); //true
        System.out.println("注意Integer equals: " + (a.equals(b)));//true

        //HashMap中Integer与int等价的
        HashMap<Integer, String> test = new HashMap<>();
        test.put(a, "我是3");
        System.out.println("注意HashMap:" + test.containsKey(b)); //true

        Zuo z1 = new Zuo(1);
        Zuo z2 = new Zuo(1);
        HashMap<Zuo, String> test2 = new HashMap<>();
        test2.put(z1, "我是z1");
        System.out.println("包装类不一样HashMap：" + test2.containsKey(z2)); //false

        HashMap<Node, String> test3 = new HashMap<>();
        Node node1 = new Node(1);
        Node node2 = node1;
        test3.put(node1, "我是node1");
        test3.put(node2, "我是node2"); //node1的值被覆盖
        System.out.println("包装类HashMap: " + test3.size()); // 1
        //node1和node2的结果相同
        System.out.println("包装类node1: " + test3.get(node1));
        System.out.println("包装类node2: " + test3.get(node2));

        System.out.println("===================");

        //哈希表，增、删、改、查的时间复杂度都是O(1)
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1000000, "我是1000000");
        map.put(2, "我是2");
        map.put(3, "我是3");
        map.put(4, "我是4");
        map.put(5, "我是5");
        map.put(6, "我是6");
        map.put(1000001, "我是1000001");
        printMap(map);

        System.out.println("有没有：" + map.containsKey(1)); //false
        System.out.println("有没有：" + map.containsKey(10)); //false

        System.out.println("获取：" + map.get(4));
        System.out.println("获取：" + map.get(10)); //null

        map.put(4, "他是4");
        System.out.println("获取：" + map.get(4));

        map.remove(4);
        System.out.println("获取：" + map.get(4));

        //HashSet 和 HashMap 性能一样
        HashSet<String> set = new HashSet<>();
        set.add("abc");
        set.contains("abc");
        set.remove("abc");

        System.out.println("===================");

        //有序表：接口名, 时间复杂度O(logN)，红黑树、avl、sb树、跳表
        //TreeMap 红黑树
        TreeMap<Integer, String> treeMap = new TreeMap<>();
        treeMap.put(3, "我是3");
        treeMap.put(4, "我是4");
        treeMap.put(8, "我是8");
        treeMap.put(5, "我是5");
        treeMap.put(7, "我是7");
        treeMap.put(1, "我是1");
        treeMap.put(2, "我是2");
        printMap(treeMap);

        //与HashMap相同的功能：增删改查
        System.out.println("有没有：" + treeMap.containsKey(1)); //true
        System.out.println("有没有：" + treeMap.containsKey(10)); //false
        System.out.println("获取：" + treeMap.get(8));
        System.out.println("获取：" + treeMap.get(10));
        treeMap.put(8, "他是8");
        System.out.println("获取：" + treeMap.get(8));
        treeMap.remove(8);
        System.out.println("获取：" + treeMap.get(8));
        printMap(treeMap);

        //特有的功能
        printMap(treeMap);
        System.out.println("第一个的key：" + treeMap.firstKey());
        System.out.println("最后一个的key: " + treeMap.lastKey());
        treeMap.remove(4);
        System.out.println("左侧的key：" + treeMap.floorKey(4)); //删除4后是3，没删4是4
        System.out.println("右侧的key" + treeMap.ceilingKey(4)); //删除4后是5，没删4是4

        //非内置类型需要实现比较器
//        TreeMap<Zuo, String> zuoMap = new TreeMap<>();
//        zuoMap.put(z1, "我是z1");
//        zuoMap.put(z2, "我是z2");
    }
}
